const DB = require('../../../../utils/db')
const JwtUtil = require('../../../../utils/jwt')
/**
 * GET,
 * 处理好友申请
 */

module.exports = async (ctx) => {
  // 获取token
  let token = ctx.request.header.authorization
  let jwt = new JwtUtil(token)
  // 用token获取到userid
  let userid = jwt.verifyToken()
  // 同意or拒绝
  const res = await DB.query(
    `update user_apply set applyStatus=${parseInt(
      ctx.request.query.applyStatus
    )} where respondentid='${userid}' and applicantid='${
      ctx.request.query.userid
    }'`
  )
  // 同意-互相添加为好友
  console.log(
    parseInt(ctx.request.query.applyStatus)===3,
    'ctx.request.query.applyStatus'
  )
  if (parseInt(ctx.request.query.applyStatus) === 3) {
    const res1 = await DB.query(
      `insert into user_friend(userid, friendid, friendStatus, createTime) values('${userid}','${ctx.request.query.userid}', 0, now()),('${ctx.request.query.userid}','${userid}',0,now())`
    )
    ctx.body = {
      code: 0,
      data: 'ok',
    }
    return
  }
    ctx.body = {
      code: 0,
      data: res,
    }
}
